package org.zfes.snowier.cloud.zbss.cms.model.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.zfes.snowier.core.data.ComboboVo;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZObjectUtil;

import com.google.common.collect.Lists;

import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.model.service.ICmsModelItemService;
import org.zfes.snowier.cloud.zbss.cms.model.service.ICmsModelService;
import org.zfes.snowier.cloud.zbss.cms.model.dao.CmsModelMapper;
import org.zfes.snowier.cloud.zbss.cms.model.dto.CmsModelDto;
import org.zfes.snowier.cloud.zbss.cms.model.model.CmsModel;
import org.zfes.snowier.cloud.zbss.cms.model.model.CmsModelItem;

import java.util.stream.Stream;
@Service
public class CmsModelServiceImpl implements ICmsModelService{
 	@Autowired
	private CmsModelMapper cmsModelMapper;
 	@Autowired
	private ICmsModelItemService cmsModelItemService;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsModel(CmsModel cmsModel) {
		ZBeanUtil.setCreateInfo(cmsModel);
		ZBeanUtil.validateBean(cmsModel);
		cmsModelMapper.insertSelective(cmsModel);
		//baseModel
		List<CmsModelItem> modelItemList=cmsModelItemService.loadCmsItemsByModelId(Long.valueOf("1"));
		cmsModelItemService.createCmsModelItemBatch(modelItemList, cmsModel.getId());
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsModel> loadCmsModelById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsModelMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsModelDto> loadCmsModelDtoById(Long id) {
		Optional<CmsModel> modelOp =loadCmsModelById( id);
		if(!modelOp.isPresent()) {
			return Optional.empty();
		}

	 	List<CmsModelItem> itemList=cmsModelItemService.loadCmsItemsByModelId( id);
	 	
		return Optional.ofNullable(new CmsModelDto(modelOp.get(),itemList));
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsModel(CmsModel cmsModel) {
		ZBeanUtil.setUpdatedInfo(cmsModel);
		ZBeanUtil.validateBean(cmsModel);
		cmsModelMapper.updateByPrimaryKeySelective(cmsModel);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsModel(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsModelMapper.updateStatus(id, Byte.valueOf("-1"));
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsModelStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsModelMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsModelDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsModelMapper.selectListCount(pm), cmsModelMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<ComboboVo> loadCmsModelComboboVo(Long siteId,boolean firstEmpty) {
		if(ZObjectUtil.isNull(siteId)) {
			return Lists.newArrayList();
		}
		List<ComboboVo> voList=cmsModelMapper.selectComboboVo( siteId);
		if(firstEmpty) {
			voList.add(0, ComboboVo.newEmptyFirst());
		}
		return voList;
	}

}